package edu.uc.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import edu.uc.bean.Goods;
import edu.uc.bean.News;
import edu.uc.dao.NewsDao;

@Repository("newsDao")
public class NewsDaoImpl extends BaseDaoImpl<News> implements NewsDao {

	@Override
	public Long insert(News bean) {
		Long result = 0L;
		try {
			// 保存
			getTmpl().save(bean);
			// 获取主键
			result = bean.getNewsId();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return result;
	}

	@Override
	public Long delete(Long id) {
		Long result = 0L;
		try {
			// 使用会话,获取特定主键的某个类型的对象
			News instance = getTmpl().get(News.class, id);
			// 如果存在,就删除
			if (instance != null) {
				getTmpl().delete(instance);
			}
			// 正常用提交,且数据存在, 则说明删除成功
			if (instance != null) {
				result = 1L;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return result;
	}

	@Override
	public Long update(News bean) {
		Long result = 0L;
		try {
			getTmpl().update(bean);
			// 正常用提交,且数据存在, 则说明删除成功
			result = 1L;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return result;
	}

	@Override
	public News load(Long id) {
		News bean = null;
		try {
			// 使用会话,获取特定类型的主键对象
			bean = getTmpl().get(News.class, id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return bean;
	}

	@Override
	public List<News> list() {
		List<News> newsList = new ArrayList<News>();
		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from News ");
		sbHql.append(" order by newsId desc ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();

		Object[] params = arrParams.toArray();
		try {

			newsList = (List<News>) getTmpl().find(hql, params);

		} catch (Exception e) {
			throw new RuntimeException(e);

		} finally {

		}
		return newsList;

	}

	@Override
	public Long count() {
		Long result = 0L;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" select count(1) from News ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		Object[] params = arrParams.toArray();
		try {
			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return result;
	}

	@Override
	public Long countByName(String name) {
		Long result = 0L;
		name = "%" + name + "%";
		StringBuilder sbHql = new StringBuilder();
		
		
		sbHql.append(" select count(1) from News where title like ? ");

		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {
			result = getTmpl().execute(this.getCallbackScalarLong(hql, params));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return result;
	}

	@Override
	public News loadByName(String name) {
		News bean = null;

		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from News where title= ? ");
		sbHql.append(" order by newsId desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);
		Object[] params = arrParams.toArray();
		try {
			bean = getTmpl().execute(this.getCallbackBean(News.class, hql, params));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
		}
		return bean;
	}

	@Override
	public List<News> pager(Long pageNum, Long pageSize) {
		List<News> newsList = new ArrayList<News>();
		StringBuilder sbHql = new StringBuilder();
		Long vStart = 0L;
		// Long vEnd = 0;

		vStart = (pageNum - 1) * pageSize;// 下标从0开始,所以不要+1
		// vEnd = pageNum * pageSize;
		Long startIndex = vStart;
		sbHql.append(" from News ");
		sbHql.append(" order by newsId asc ");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();

		Object[] params = arrParams.toArray();
		try {
			newsList = getTmpl().execute(this.getCallbackPager(News.class, startIndex, pageSize, hql, params));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return newsList;
	}

	@Override
	public List<News> pagerByName(String name, Long pageNum, Long pageSize) {
		List<News> newsList = new ArrayList<News>();

		Long vStart = 0L;
		vStart = (pageNum - 1) * pageSize;// 不要+1

		name = "%" + name + "%";
		Long startIndex = vStart;
		StringBuilder sbHql = new StringBuilder();
		sbHql.append(" from News ");
		sbHql.append(" where title like ? ");
		sbHql.append(" order by newsId desc");
		String hql = sbHql.toString();

		List<Object> arrParams = new ArrayList<Object>();
		arrParams.add(name);

		Object[] params = arrParams.toArray();
		try {
			newsList = getTmpl().execute(this.getCallbackPager(News.class, startIndex, pageSize, hql, params));
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {

		}
		return newsList;
	}
}
